<?php

namespace App\Http\Controllers\Mobile\Zhitiao;

use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Zhitiao;
use App\Models\Zhitiao_msg;
use App\Models\UsersReg;

//use Illuminate\View\View;
use Session;
use Redirect;
use DB;

class ZhitiaoController extends Controller
{
    public function zhitiao()
    {
        //纸条列表
        $zhitiaos = DB::select("select p.id, count(p.id) as message_count, p.user_id, p.friend_id, p.fa_id, p.shou_id, p.send_time, p.msg_id, p.status, u.user_name as receiver_name, u.avatar_file as receiver_image from (select * from yds_zhitiao order by id desc) p INNER JOIN yds_users u on u.uid = friend_id where p.user_id = ". Session::get('nameid') ." and p.status != 3 group by p.friend_id order by p.id desc");

        //dd($zhitiaos);

        foreach($zhitiaos as $zhitiao) {            
            //最后一纸条内容
            $zhitiao->content = Zhitiao_msg::find($zhitiao->msg_id);
        }

        foreach($zhitiaos as $zhitiao) {
            //未读信息总数,status//消息状态，1：未读 2：已读 3：删除
            $zhitiao->weidu = Zhitiao::where('user_id', Session::get('nameid'))->where('friend_id', $zhitiao->friend_id)->where('status', 1)->count();
        }

        return view('mobile.zhitiao.zhitiao',array(
            'zhitiaos' => $zhitiaos
        ));
    }

    public function msgs($userid)
    {
        //纸条交流信息

        if($userid == Session::get('nameid'))
        {
            //不能给自己发信息
            return '不可以给自己发纸条！';
        }

        $users = UsersReg::find($userid);

        if($users == null)
        {
            //如果会员不存在
            abort(404);
        }

        $zhitiaos = DB::select("select p.id, p.msg_id, p.fa_id, p.shou_id, p.status, p.send_time, u.user_name AS sender_name,u.avatar_file AS sender_image, uu.user_name AS receiver_name FROM yds_zhitiao p INNER JOIN yds_users u ON u.uid = p.fa_id INNER JOIN yds_users uu ON uu.uid = p.friend_id WHERE p.user_id = ". Session::get('nameid') ." AND p.friend_id = ". $userid ." AND p.status !=3 ORDER BY p.id DESC");

        //dd($zhitiaos);

        //统计纸条信息数量
        $message_count = Zhitiao::where('user_id', Session::get('nameid'))->where('friend_id', $userid)->where('status','!=',3)->count();

        //标记已读信息
        DB::update("update yds_zhitiao set status = 2 WHERE user_id = ". Session::get('nameid') ." AND friend_id = ". $userid . " AND status = 1");

        foreach($zhitiaos as $zhitiao) {
            //发送的信息内容
            $zhitiao->content = zhitiao_msg::find($zhitiao->msg_id);
        }

        //dd($message_count);

        //收件人
        $user_name = UsersReg::find($userid)->user_name;        

        return view('mobile.zhitiao.msgs', array(
            'zhitiaos' => $zhitiaos,
            'user_name' => $user_name,
            'message_count' => $message_count
        ));
    }

    public function save_msgs($userid)
    {
        //保存发送的纸条信息
        $zhitiao_msg = new Zhitiao_msg;

        $zhitiao_msg->content = request::input('fazhitiao'); //消息内容
        $zhitiao_msg->save(); //保存消息内容

        $msgid = $zhitiao_msg->id; //消息的id

        $zhitiao = new Zhitiao;

        $zhitiao->fa_id = Session::get('nameid'); //发件人
        $zhitiao->shou_id = $userid;  //收件人
        //未调换id
        $zhitiao->user_id = Session::get('nameid');//非真实的发件人
        $zhitiao->friend_id = $userid; //非真实的收件人

        $zhitiao->msg_id = $msgid; //对应的消息内容的id 
        $zhitiao->msg_type = 1; //消息类型，1普通消息，2系统消息
        $zhitiao->status = 1; //消息状态， 1：未读 2：已读 3：删除
        $zhitiao->send_time = time();
        $zhitiao->save(); //第一次保存

        ///////////////////////////
        $zhitiao = new Zhitiao;

        $zhitiao->fa_id = Session::get('nameid'); //发件人
        $zhitiao->shou_id = $userid;  //收件人
        //调换id
        $zhitiao->user_id = $userid;//非真实的发件人
        $zhitiao->friend_id = Session::get('nameid'); //非真实的收件人

        $zhitiao->msg_id = $msgid; //对应的消息内容的id 
        $zhitiao->msg_type = 1; //消息类型，1普通消息，2系统消息
        $zhitiao->status = 1; //消息状态， 1：未读 2：已读 3：删除
        $zhitiao->send_time = time();
        $zhitiao->save(); //第二次保存

        return Redirect('mobile/zhitiao/'. $userid);

    }

    public function delmsg($id)
    {
        //删除对应的纸条
        $zhitiao = Zhitiao::where('id',$id)->where('user_id',Session::get('nameid'))->first();

        if($zhitiao == null)
        {
            //无权限删除别人的消息
            return '没有删除权限！';
        }

        $zhitiao = Zhitiao::find($id); //消息状态， 1：未读 2：已读 3：删除
        $zhitiao->status = 3;
        $zhitiao->save();
        return Redirect()->back();
    }

    public function delallmsg($userid)
    {
        //删除对话的全部纸条
        $uid = Session::get('nameid');     
        
        $zhitiao = Zhitiao::where('friend_id', $userid)
            ->where('user_id', $uid)->update(array('status' => 3));//消息状态， 1：未读 2：已读 3：删除
        return Redirect()->back();
    }

    public function read_no($view)
    {
        //导航条头部的显示未读的信息数量(公共部分)
        $weidu = Zhitiao::where('user_id', Session::get('nameid'))->where('status', 1)->count();
        $view->with('weidu',  $weidu);
    }

}
